﻿using amuudp.package;
using amuudp.sender;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace amuudp
{
    public class UdpServer
    {
        private UdpClient client;

        public int ClientId { get; private set; }

        private Dictionary<int, Package> packages = new Dictionary<int, Package>();
        private IServerEvents events;

        private Thread receiveThread;

        public int Port { get; private set; }

        public UdpServer(int clientId, int port, IServerEvents events)
        {
            this.ClientId = clientId;
            this.Port = port;
            this.events = events;
        }

        public void Start()
        {
            createReceiveThread();
        }

        public void Stop()
        {
            receiveThread.Abort();
            client.Close();
        }

        private void createReceiveThread()
        {
            if(receiveThread != null)
            {
                receiveThread.Abort();
            }

            receiveThread = new Thread(() => ReceiveDo());
            receiveThread.Start();
        }

        private void ReceiveDo()
        {
            client = new UdpClient(Port);//本地端口监听
            IPEndPoint idpRecive = new IPEndPoint(IPAddress.Any, 0);//需要监听的地址和端口号  any标示所有ip

            while (true)//无限循环，也就是不停的监听
            {
                byte[] reciveByte = client.Receive(ref idpRecive);//udp收取信息 字节符
                Fragment fragment = new Fragment(reciveByte);
                events.OnReceive(fragment, idpRecive.Address, idpRecive.Port, this);
                AddFragment(fragment);

                if (packages[fragment.PackageId].Complete)
                {
                    events.OnPackageComplete(packages[fragment.PackageId], idpRecive.Address, idpRecive.Port, this);
                    packages.Remove(fragment.PackageId);
                }
            }
        }

        public void Send(string message, IPEndPoint iPEndPoint)
        {
            Sender sender = new MessageSender(ClientId, message, iPEndPoint);
            while (sender.SendNext())
            {

            }
            sender.Close();
        }

        public void Send(Bitmap bitmap, int imageId, IPEndPoint iPEndPoint)
        {
            ImageSender sender = new ImageSender(ClientId, imageId, bitmap, iPEndPoint);
            while (sender.SendNext())
            {

            }
        }

        public void Send(byte[] imageBytes, int imageId, IPEndPoint iPEndPoint)
        {
            ImageSender sender = new ImageSender(ClientId, imageId, imageBytes, iPEndPoint);
            while (sender.SendNext())
            {

            }
        }

        // 添加片段
        public void AddFragment(Fragment fragment)
        {
            if (packages.ContainsKey(fragment.PackageId))
            {
                packages[fragment.PackageId].AddFragment(fragment);
            }
            else
            {
                packages.Add(fragment.PackageId, new Package(fragment));
            }
        }
    }

}
